#include <iostream>
using namespace std;

//函数模板语法

//两个整型交换函数
void swapInt(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
//两个浮点型交换函数
void swapDouble(double& a, double& b)
{
    double temp = a;
    a = b;
    b = temp;
}

//函数模板
template<typename T>    //声明一个模版，告诉编译器后面代码中的T不要报错 template <typename T1, typename T2, …>
void myswap(T& a, T& b)   //T为通用数据类型
{
    T temp = a;
    a = b;
    b = temp;
}

//使用普通函数交换
void test1()
{
    int a = 10;
    int b = 99;
    swapInt(a, b);

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    double c = 1.1;
    double d = 2.3;
    swapDouble(c, d);
    cout << "c = " << c << endl;
    cout << "d = " << d << endl;
}

//使用函数模版交换
void test2()
{
    //两种方式使用函数模版
    //1、自动类型推导
    int a = 10;
    int b = 99;
    myswap(a, b);
    
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    //2、显示指定类型
    double c = 1.1;
    double d = 2.3;
    
    myswap<double>(c, d);//指定T 为double
    cout << "c = " << c << endl;
    cout << "d = " << d << endl;
}

int main()
{
    test2();
    return 0;
}